package com.springboot.demo.web;

import com.springboot.demo.entity.FineMsg;
import com.springboot.demo.entity.IncomeRecord;
import com.springboot.demo.service.FineMsgService;
import com.springboot.demo.service.IncomeRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;

@Controller
public class FineMsgController {

    @Autowired
    HttpServletRequest request;
    @Autowired
    FineMsgService fineMsgService;

    @Autowired
    IncomeRecordService incomeRecordService;

    @GetMapping("/finemsg")
    public String getFineMsg(Model model){
        String phone=(String)request.getSession().getAttribute("phonenumber");
        System.out.println("-------------------------"+phone);
        List<FineMsg> finemsgs=fineMsgService.getFineMsg();
        model.addAttribute("fmsgs",finemsgs);
        return "librarian_fine";
    }

    @Transactional
    @PostMapping("/updatefine/{phone}/{fine}/{initdate}/{bookid}")
    public String Updatefine(@PathVariable("phone")String phone,@PathVariable("fine")int fine,@PathVariable("initdate") String initdate,@PathVariable("bookid") long bookid){
        fineMsgService.updatefine(phone, fine, initdate, bookid);

        //增加图书馆的收入记录---罚金收入
        IncomeRecord incomeRecord = new IncomeRecord();
        incomeRecord.setPhone(phone);
        incomeRecord.setCategory("fine");
        incomeRecord.setMoney(fine);
        incomeRecord.setDate(new Date());
        incomeRecordService.save(incomeRecord);

        return "redirect:/finemsg";
    }

}
